package com.dcy.znoa.service.publiz.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.SelectBaseModel;
import com.dcy.znoa.controller.publiz.model.duty.SelectDutyModel;
import com.dcy.znoa.dao.publiz.CompanyDao;
import com.dcy.znoa.dao.publiz.DutyDao;
import com.dcy.znoa.dao.user.OperDao;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.publiz.Company;
import com.dcy.znoa.domain.publiz.Duty;
import com.dcy.znoa.service.publiz.DutyService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Service
public class DutyServiceImpl implements DutyService {

    @Autowired
    DutyDao dutyDao;
    @Autowired
    CompanyDao companyDao;
    @Autowired
    OperDao operDao;

    @Override
    public JsonForList selectList(SelectDutyModel model) throws InvocationTargetException, IllegalAccessException {
        EntityWrapper<Duty> ew = new EntityWrapper<>();
        Duty cp = new Duty();
        BeanUtils.copyProperties(cp, model);
        cp.setDutyName(null);
        ew.setEntity(cp);
        ew.like("duty_name", model.getDutyName());
        Integer total = SelectBaseModel.PageFill(model,ew,dutyDao);//加入分页参数
//        List<Duty> list = dutyDao.selectListMapper(ew);
        List<Duty> list;
        Page page = new Page(model.getPageNum(), model.getPageSize());
        list = dutyDao.selectPage(page, ew);
        for(Duty duty:list){
            Company company = companyDao.selectById(duty.getCompId());
            duty.setCompany(company);
        }
        return new JsonForList(list,page.getTotal());
    }

    @Override
    public JsonCommon deleteById(Duty cp) throws BusinessException {
        //查询有没有用户绑定过
        List list = operDao.selectList(new EntityWrapper<Oper>().eq("duty_id",cp.getDutyId()));
        if(!list.isEmpty()){
            throw  new BusinessException("804","此职务已经绑定用户，不允许删除");
        }
        Integer result = dutyDao.deleteById(cp.getDutyId());
        if (result > 0){
            return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
        }
        return new JsonCommon(JsonCommon.DELETE_ERROR_CODE);
    }

    @Override
    public JsonCommon insert(Duty var1) {
        //查询：同一个公司下不允许有同名的职位
        List list = dutyDao.selectList( new EntityWrapper<Duty>().eq("comp_id",var1.getCompId()).eq("duty_name",var1.getDutyName()));
//        List list = dutyDao.selectList( new EntityWrapper<Duty>().eq("comp_id",var1.getCompId()).eq("duty_name",var1.getDutyName()).eq("valid_flag",1));
        if(!list.isEmpty()){
            return new JsonCommon(JsonCommon.INSERT_ERROR_CODE,"不允许有同名的职位");
        }
        Integer result = dutyDao.insert(var1);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.INSERT_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }

    @Override
    public JsonCommon updateById(Duty var1) {
        //查询当前角色对应的企业ID
        Duty duty = dutyDao.selectById(var1.getDutyId());
        if(duty.getCompId()==null){
            return  new JsonCommon("","请联系管理员先为此职务绑定一个公司");
        }
        //检查是否有职位名，查询库里同名的职位是否有不同的id
        List<Duty> list = dutyDao.selectList( new EntityWrapper<Duty>().eq("comp_id",duty.getCompId()).eq("duty_name",var1.getDutyName()).eq("valid_flag",1));
        boolean flag;
        if(list.isEmpty()){ //允许修改
           flag = true;
        }else if(list.size()==1){//判断id是不是一样的
            if(var1.getDutyId().longValue() == list.get(0).getDutyId().longValue()){//允许修改
                flag = true;
            }else{
                flag = false;
            }
        }else{// >1，不允许修改
            flag = false;
        }
        if(flag == false){
            return new JsonCommon(JsonCommon.UPDATE_ERROR_CODE,"存在同名职位，不允许修改");
        }
        //如果职务有对应的员工，则不允许禁用
        if(var1.getValidFlag() == 0){
            //查询有没有员工
            List<Oper> operList = operDao.selectList( new EntityWrapper<Oper>().eq("duty_id",var1.getDutyId()).eq("valid_flag",1));
            if(!operList.isEmpty()){
                return new JsonCommon(JsonCommon.UPDATE_ERROR_CODE,"该职务对应有员工，不允许禁用");
            }
        }
        Integer result = dutyDao.updateById(var1);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.UPDATE_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }
}
